# -*- coding: utf-8 -*-
import logging

from django import http
from django import template
from django.conf import settings
from django.template import loader
from django.http import HttpResponseRedirect

from common import commonfunction
from database import function_cloud
from database import function_user

URL_COMMEND_MORE = "/commend"
URL_COLLECT_MORE = "/collect"

def Index(request):
    Response = http.HttpResponse()
    Response['Content-Language'] = commonfunction.ResetLanguage(request)
    
    TemplateValues = commonfunction.GetDefaultTemplateValue(request)
    CommendItems = function_cloud.GetHotCommendRecord(settings.PAGE_SIZE)
    CollectItems = function_cloud.GetHotCollectRecord(settings.PAGE_SIZE)
    
    CloudCommend = {"ID":"0", "Title":"Most Popular", "URL":URL_COMMEND_MORE, "SubItems":CommendItems}
    CloudCollect = {"ID":"1", "Title":"Most Favorites", "URL":URL_COLLECT_MORE, "SubItems":CollectItems}
    TemplateValues.update( { 'CloudCommend':CloudCommend, 'CloudCollect':CloudCollect, } )
    
#    CommendItems = ({"Title":"test1","SourceUrl":"www.aa.com","Date":"2010-04-02","CommendNumber":"6","Summary":"摘要1"},
#                {"Title":"测试2","SourceUrl":"bb","Date":"2010-04-03","CollectNumber":"9","Summary":"摘要2"})
#    CollectItems = ({"Title":"1111","SourceUrl":"www.a.com","Date":"2010-04-01","from":"tianya","notes":"摘要3"},
#                 {"Title":"2222","SourceUrl":"www.b.com","Date":"2010-04-02","from":"tianya","notes":"摘要4"})
    Categories = ({"id":"0","title":"Most Popular","url":"www.c.com","SubItems":CommendItems},
                  {"id":"1","title":"Most Favorites","url":"www.c.com","SubItems":CollectItems},)
    TemplateValues.update( { 'HotCtgs':Categories,} )

    Tags = function_cloud.GetHotCommendRecord(settings.PAGE_SIZE)
    Tags = ({"TagName":"google app","SearchNumber":"5"},{"TagName":"yoyo","SearchNumber":"8"},{"TagName":"美","SearchNumber":"3"},
            {"TagName":"溜溜网","SearchNumber":"15"},{"TagName":"abc","SearchNumber":"2"},
            {"TagName":"杨玉奇","SearchNumber":"8"},{"TagName":"baidu","SearchNumber":"8"},{"TagName":"杨玉奇","SearchNumber":"8"}
            )
    TemplateValues.update({'Tags':Tags,})
    
    c = template.RequestContext(request,TemplateValues)
    t = loader.get_template("index.html")
    Response.write(t.render(c))
    return Response

def UserPhoto(request):
    UserID = request.getParameter("item").trim()
    UserInfo = function_user.GetOneUserInfo(UserID)
    Photo = None
    if UserInfo:
        if UserInfo.HavePhoto:
            Photo = UserInfo.Photo

    Response = http.HttpResponse()
    Response['Content-Type'] = 'image/jpeg'
    if Photo:
        Response.write(Photo)
    else:
        HttpResponseRedirect('/images/defaultphoto.jpg')


def Err_404(request, template_name='404.html'):
  # You need to create a 404.html template.
  t = loader.get_template(template_name)
  return http.HttpResponseNotFound(
      t.render(template.RequestContext(request, {'request_path': request.path})))


def Err_500(request, template_name='500.html'):
  logging.error("An error occurred: %s", str(request))
  # You need to create a 500.html template.
  t = loader.get_template(template_name)
#  return http.HttpResponseServerError(
#      t.render(template.RequestContext(request, {})))
  return http.HttpResponse(t.render(template.RequestContext(request, {})))
